import 'package:flutter/material.dart';
import 'package:osc/common/osc_user_manager.dart';
import 'package:osc/event/event.dart';
import 'package:osc/event/event_constant.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/res/strings.dart';
import 'package:osc/ui/pages/account/login_page.dart';
import 'package:osc/ui/pages/post/post_list.dart';
import 'package:osc/ui/pages/project/project_recommedn_list_fragment.dart';
import 'package:osc/ui/pages/search/search.dart';
import 'package:osc/ui/widgets/common_tab_bar.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';

///综合
class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new HomeState();
  }
}

class HomeState extends State<Home> with TickerProviderStateMixin, AutomaticKeepAliveClientMixin<Home> {
  double width = 0;
  double height = 0;

  TabController tabController;
  int currentTabIndex = 0;
  int tempTabIndex = 0;


  @override
  void initState() {
    super.initState();
    onBusEvent();
    tabController = TabController(length: 4, vsync: this);
    tabController.addListener(() {
      currentTabIndex = tabController.index;
      if (tempTabIndex == currentTabIndex) {
        return;
      }
      tempTabIndex = currentTabIndex;
    });
  }

  @override
  void dispose() {
    tabController?.dispose();
    offBusEvent();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if(width == 0) {
      Size size = MediaQuery.of(context).size;
      width = size.width;
      height = size.height;
    }
    return CommonTabBar(
      scrollable: false,
      actions: <Widget>[
        SmallWidgets.buildActionIcon(
          imageName: "ic_search",
          bgColor: Colors.transparent,
          onTap: (){
            jumpToPageForResult(Search(showLeading: true,));
          }
        )
      ],
        tabs: [
//          Tab(text:"推荐"),
          Tab(text:"资讯"),
          Tab(text:"博客"),
          Tab(text:"软件"),
          Tab(text:"问答"),
        ],
        pages: [
//          Text("推荐"),
          PostList(catalog: 1, withBanner: true, richTextTitle: true,),
          PostList(catalog: 4, withBanner: true, richTextTitle: true,),
          ProjectRecommendListFragment(),
          PostList(catalog: 8, showPortrait: true,),
        ],
    );
  }


  buildLine() {
    return Container(
      color: Colours.gray_f5,
      height: Dimens.gap_view_normal,
    );
  }


  jump2PageForResult(Widget page, {bool checkLogin = false}) {
    if(checkLogin){
      if(OscUserManager.getInstance().isLogin()){
        jumpToPageForResult(page);
      } else {
        jumpToPageForResult(LoginPage());
        showTips(StringIds.userNoLogin);
      }
    } else {
      jumpToPageForResult(page);
    }
  }
  jumpToPageForResult(Widget page) {
    NavigatorUtil.pushPageForResult<PopResult>(context, page).then((result) {
      if (result != null) {
        switch (result.id) {
          case PopResult.ID_LOGIN:
            break;
        }
      }
    });
  }

  showTips(String msg){
    PdUtil.showToast(msg: msg);
  }

  onBusEvent(){
    eventBus.on(EventConstant.LOGIN, (tag){
      if(mounted) {
        setState(() {

        });
      }
    });
    eventBus.on(EventConstant.LOGOUT, (tag){
      if(mounted) {
        setState(() {

        });
      }
    });
  }
  offBusEvent(){
    eventBus.off(EventConstant.LOGIN);
  }

  @override
  bool get wantKeepAlive => true;
}
